<template>
  <div class="warnList">
    <el-card>
      <el-form inline :model="pageParams">
        <el-form-item label="一体杆名称">
          <el-input size="small" placeholder="请输入一体杆名称" v-model="pageParams.poleName"></el-input>
        </el-form-item>
        <el-form-item label="一体杆编号">
          <el-input size="small" placeholder="请输入一体杆编号" v-model="pageParams.poleNumber"></el-input>
        </el-form-item>
        <el-form-item label="处置状态">
          <el-select size="small" v-model="pageParams.handleStatus" placeholder="请选择处置状态">
            <el-option  v-for="(item) in statusArray" :value="item.value" :label="item.label" :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table border size="small" stripe :header-cell-style="{background:'#4d8ed9',color:'#fff'}" :data="warningList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column label="一体杆名称" prop="poleName" align="center"></el-table-column>
        <el-table-column label="一体杆编号" prop="poleNumber" align="center"></el-table-column>
        <el-table-column label="故障类型" prop="errorType" align="center"></el-table-column>
        <el-table-column label="处置状态" align="center">
          <template #default="{row}">
            <el-tag effect="dark" :type="formatTagType(row.handleStatus)">{{ formatterStatus(row.handleStatus) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="告警时间" prop="warningTime" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" :disabled="row.handleStatus!==0">派单</el-button>
            <el-button type="text">详情</el-button>
            <el-button type="text" @click="handleDelete(row)" :disabled="row.handleStatus===1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[15, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { statusArray } from '@/constant/rod/warnList'

export default {
  name: 'warnList',
  data() {
    return {
      /* 获取告警列表的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 15,
        poleName: undefined,
        poleNumber: undefined,
        handleStatus: null
      },
      loading: false,
      statusArray
    }
  },
  methods: {
    /* 发送请求获取告警列表的回调函数 */
    async fetchWarningList() {
      try {
        this.loading = true
        await this.$store.dispatch('warnList/fetchWarningList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    formatterStatus(handleStatus) {
      const MAP = { 0: '未派单', 1: '已派单', 2: '已接单', 3: '已完成' }
      return MAP[handleStatus] || '未知'
    },
    formatTagType(handleStatus) {
      const MAP = { 0: 'warning', 1: 'primary', 2: 'info', 3: 'success' }
      return MAP[handleStatus]
    },
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchWarningList()
    },
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchWarningList()
    },
    handleSearch() {
      this.pageParams.page = 1
      this.fetchWarningList()
    },
    async handleDelete(row) {
      try {
        const result = await this.$store.dispatch('warnList/deleteWarningList', row.id)
        await this.fetchWarningList()
        this.$message.success(result)
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  computed: {
    ...mapGetters('warnList', ['warningList', 'total'])
  },
  created() {
    this.fetchWarningList()
  }
}
</script>

<style scoped lang="scss">
.warnList {
  .el-card {
    margin-bottom: 10px;

    .el-form-item {
      margin-bottom: unset;
    }

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>

